#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/6/3 19:02
# @Author  : LiShan
# @Email   : lishan_1997@126.com
# @File    : 3d.py
# @Note    : this is note
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

font = {'family' : 'SimSun',
    'weight' : 'bold',
    'size'  : '16'}

dqn_record_file = "./txt/test_dqn_record.txt"
fix_record_file = "./txt/test_fix_record.txt"

# Fixing random state for reproducibility
np.random.seed(19680801)


def randrange(n, vmin, vmax):
    """
    Helper function to make an array of random numbers having shape (n, )
    with each number distributed Uniform(vmin, vmax).
    """
    return (vmax - vmin)*np.random.rand(n) + vmin

# fig = plt.figure()
# ax = fig.add_subplot(projection='3d')
#
# n = 100

# For each set of style and range settings, plot n random points in the box
# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
# for m, zlow, zhigh in [('o', -50, -25), ('^', -30, -5)]:
#     xs = randrange(n, 23, 32)
#     ys = randrange(n, 0, 100)
#     zs = randrange(n, zlow, zhigh)
#     ax.scatter(xs, ys, zs, marker=m)

# ax.set_xlabel("Step", fontproperties="SimSun", size=10.5)
# ax.set_ylabel("Plan", fontproperties="SimSun", size=10.5)
# ax.set_zlabel("Delay", fontproperties="SimSun", size=10.5)

# ax.set_xlabel("Step")
# ax.set_ylabel("Plan")
# ax.set_zlabel("Delay")

# ax.set_xlabel('X Label')
# ax.set_ylabel('Y Label')
# ax.set_zlabel('Z Label')

# plt.savefig("./png/3d.jpg", dpi=600)
# # plt.show()
# plt.close()

if __name__ == '__main__':
    # dqn 延误
    names = ["step", "plan", "delay", "reward", "state"]
    data = pd.read_csv(dqn_record_file, error_bad_lines=False, sep="\t+", names=names, engine="python")
    dqn_step = list(data["step"].values)
    dqn_plan = list(data["plan"].values)
    dqn_delay = list(data["delay"].values)
    mean_delay = sum(dqn_delay) / len(dqn_delay)
    mean_delay = round(mean_delay, 3)

    # fix 延误
    names = ["plan", "step", "delay", "reward"]
    data = pd.read_csv(fix_record_file, error_bad_lines=False, sep="\s+", names=names)
    data.sort_values(by='delay', inplace=True)
    fix_plan = list(data["plan"].values)
    fix_delay = list(data["delay"].values)
    fix_min_idx = fix_plan[fix_delay.index(min(fix_delay))]
    fix_max_idx = fix_plan[fix_delay.index(max(fix_delay))]

    # min-fix 延误
    min_fix_file = fix_record_file[:-4] + '_' + str(fix_min_idx) + '.txt'
    print("读取文件：", min_fix_file)
    names = ["step", "plan", "delay", "reward", "state"]
    data = pd.read_csv(min_fix_file, error_bad_lines=False, sep="\t+", names=names, engine="python")
    fix_min_step = list(data["step"].values)
    fix_min_plan = list(data["plan"].values)
    fix_min_delay = list(data["delay"].values)

    # max-fix 延误
    max_fix_file = fix_record_file[:-4] + '_' + str(fix_max_idx) + '.txt'
    print("读取文件：", max_fix_file)
    names = ["step", "plan", "delay", "reward", "state"]
    data = pd.read_csv(max_fix_file, error_bad_lines=False, sep="\t+", names=names, engine="python")
    fix_max_step = list(data["step"].values)
    fix_max_plan = list(data["plan"].values)
    fix_max_delay = list(data["delay"].values)

    fig = plt.figure()
    ax = fig.add_subplot(projection='3d')
    # ax.scatter(dqn_step, dqn_plan, dqn_delay, color='black', marker='^')
    # ax.scatter(fix_min_step, fix_min_plan, fix_min_delay, color='gray', marker='o')
    # ax.scatter(fix_max_step, fix_max_plan, fix_max_delay, color='gray', marker='*')

    ax.scatter(dqn_plan, dqn_step, dqn_delay, color='black', marker='^')
    ax.scatter(fix_min_plan, fix_min_step, fix_min_delay, color='gray', marker='o')
    ax.scatter(fix_max_plan, fix_max_step, fix_max_delay, color='gray', marker='*')

    # ax.scatter(dqn_step, dqn_plan, dqn_delay, marker='^')
    # ax.scatter(fix_min_step, fix_min_plan, fix_min_delay, marker='o')

    # 设置坐标轴范围
    ax.set_ylim([-1, 100])
    ax.set_xlim([-1, 20])
    # 设置坐标轴刻度
    ax.set_yticks(range(0, 120, 20))
    ax.set_xticks(np.arange(0, 25, 5))

    plt.tick_params(labelsize=8.5)
    labels = ax.get_xticklabels() + ax.get_yticklabels() + ax.get_zticklabels()
    # print labels
    [label.set_fontname('Times New Roman') for label in labels]

    # ax.set_xlabel("测试步长", fontproperties="SimSun", size=10.5)
    # ax.set_ylabel("配时方案", fontproperties="SimSun", size=10.5)
    # ax.set_zlabel("延误时间", fontproperties="SimSun", size=10.5)

    ax.set_ylabel("Step", fontproperties="Times New Roman", size=8.5)
    ax.set_xlabel("Plan", fontproperties="Times New Roman", size=8.5)
    ax.set_zlabel("Delay", fontproperties="Times New Roman", size=8.5)

    ax.view_init(elev=10, azim=-30)

    plt.savefig("./png/3d.jpg", dpi=600)
    plt.show()
    plt.close()
